import data
import regrese
import matplotlib.pyplot as plt
import numpy as np
import imp


#prekompilovani kodu
imp.reload(regrese)
imp.reload(data)

#=========================GENEROVANI DAT
zadanaTHETA=[2,10,2,10]
min=-100
max=100
[x,y]=data.polynom(min,max,zadanaTHETA,1)


dataP=plt.subplot(2,3,1)
dataP.plot(x,y, "b.")
titleStr="y="
for i in range(0,len(zadanaTHETA)):
	titleStr+="+"+str(zadanaTHETA[i])+"x^"+str(i)
dataP.set_title(titleStr)
plt.xlabel('X')
plt.ylabel('Y')

#=========================REGRESE
#[theta,E,it]=regrese.puleniIntervalu(x, y, 50000, [50,50])
[theta, E, it, thetaS, XS]=regrese.gradientDescent(x,y,3,0.01,0.01)
'''
[theta, E, it, thetaS, XS]=regrese.gradientDescent(x,y,3,0.01,0.01)
[theta, E, it, thetaS, XS]=regrese.gradientDescent(x,y,5,0.01,0.01)
[theta, E, it, thetaS, XS]=regrese.gradientDescent(x,y,7,0.01,0.001)
[theta, E, it, thetaS, XS]=regrese.gradientDescent(x,y,9,0.01,0.0001)
'''

#=========================ZOBRAZENI VYSLEDKU
#==========vykresleni prolozene funkce
osa=np.arange(min,max,0.1)
X=np.zeros((len(theta[it]),len(osa)))
for i in range(0,len(theta[it])):
	X[i]=pow(osa,i)
dataP.plot(osa, np.dot(theta[it],X), "r");
#==========vykresleni vyvoje chyby

conv=plt.subplot(2,3,2)
conv.plot(range(0,it+1),E,"r.--")
conv.set_title("Convergence")
#==========vykresleni vyvoje parametru theta

for i in range(0,len(theta[it])):
	cost=plt.subplot(2,len(theta[it]),len(theta[it])+i+1)
	cost.set_title("theta"+str(i)+": "+str(round(theta[it][i])))
	cost.plot(theta[:,i],"r.--")

#==========vykresleni chybove plochy
'''
error=plt.subplot(2,3,3)
okoli=10
theta00, theta11 = np.meshgrid(np.arange(thetaS[it,0]-okoli,thetaS[it,0]+okoli,1)\
			      ,np.arange(thetaS[it,1]-okoli,thetaS[it,1]+okoli,1))
#E = 1.0/(2*len(x))*sum((theta00*pow(x,0)+theta11*pow(x,1)-y)**2)
ErSurface=np.zeros([len(theta00[:,0]), len(theta11[0,:])])
for i in range(0,len(ErSurface[:,0])):
	for j in range(0,len(ErSurface[0,:])):
		ErSurface[i,j]=1.0/(2*len(XS[0]))*sum(((theta00[i,j]*XS[0,:]+theta11[i,j]*XS[1,:])-y)**2)

error.contour(theta00,theta11,ErSurface,30)
error.plot(thetaS[:,0],thetaS[:,1],'r.--')
error.set_title("standardized space")
plt.xlabel('theta0')
plt.ylabel('theta1')
'''
plt.show()
